package com.mes.loco.aps.server.controller.aps;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.APSService;
import com.iplant.aps.service.MyHelperService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSSchedulingVersionBPM;
import com.iplant.aps.service.po.aps.APSTaskPart;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.bpm.BPMTaskBase;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 周/月计划审批(流程引擎版)
 *
 * @author PengYouWang
 * 2020-7-14 23:08:13
 */
@RestController
@RequestMapping("/api/APSSchedulingVersionBPM")
public class APSSchedulingVersionBPMController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(APSSchedulingVersionBPMController.class);

    final
    APSService wAPSService;

    final MyHelperService wMyHelperService;

    public APSSchedulingVersionBPMController(APSService wAPSService, MyHelperService wMyHelperService) {
        this.wAPSService = wAPSService;
        this.wMyHelperService = wMyHelperService;
    }

    /**
     * 查历史记录
     */
    @GetMapping("/History")
    public Object History(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            int wUpFlowID = StringUtils.parseInt(request.getParameter("UpFlowID"));
            int wFollowerID = StringUtils.parseInt(request.getParameter("FollowerID"));
            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<APSSchedulingVersionBPM>> wServiceResult = wAPSService
                    .APS_QuerySchedulingVersionBPMHistory(wLoginUser, wID, wCode, wUpFlowID, wFollowerID,
                            wAPSShiftPeriod, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 人员获取任务
     */
    @GetMapping("/EmployeeAll")
    public Object EmployeeAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wTagTypes = StringUtils.parseInt(request.getParameter("TagTypes"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));

            ServiceResult<List<BPMTaskBase>> wServiceResult = wAPSService.APS_QuerySchedulingVersionBPMEmployeeAll(
                    wLoginUser, wAPSShiftPeriod, wTagTypes, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取待办、已办、发起 月、周计划审批任务
     */
    @GetMapping("/EmployeeAllWeb")
    public Object EmployeeAllWeb(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            ServiceResult<List<APSSchedulingVersionBPM>> wServiceResult = wAPSService
                    .APS_QuerySchedulingVersionBPMEmployeeAllWeb(wLoginUser, wAPSShiftPeriod, wStatus, wOrderID,
                            wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取周/月计划审批单子项详情
     */
    @GetMapping("/Details")
    public Object Details(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wAPSSchedulingVersionBPMID = StringUtils.parseInt(request.getParameter("APSSchedulingVersionBPMID"));

            ServiceResult<List<APSTaskPart>> wServiceResult = wAPSService
                    .APS_QuerySchedulingVersionItemDetails(wLoginUser, wAPSSchedulingVersionBPMID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result,
                        wServiceResult.CustomResult.get("Info"));
                this.SetResult(wResult, "OMSOrderList", wServiceResult.CustomResult.get("OMSOrderList"));
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 同步预期到货时间
     */
    @GetMapping("/SynchronizeExpectTime")
    public Object SynchronizeExpectTime(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wAPSBPMID = StringUtils.parseInt(request.getParameter("APSBPMID"));

            if (wAPSBPMID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult =
                    wMyHelperService.APS_SynchronizeExpectTime(wLoginUser, wAPSBPMID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
